var commonCounter = 0;
var totalPrice = 0;
var itemCode = {}, itemName = {}, batchId = {}, mfd = {}, exp = {}, boughtPrice = {}, unitPrice = {}, quantity = {},objCounter = {};
function validate(i){
	var mfd = $('#mfd'+i).val();
	var exp = $('#exp'+i).val();
	if ($("#batchId"+i+" option:selected").val() == 0){
		$("#messages").html("<div id='msg1' title='Indika Stores'>Batch date is required.");
		//$( "#msg1" ).dialog();
		$( "#msg1" ).dialog({buttons: { "Ok": function() { $(this).dialog("close"); } }, draggable: true, hide: "slide", show: "slide", width: 470 });
	}else if($('#boughtPrice'+i).val() == '' ){
		$("#messages").html("<div id='msg2' title='Indika Stores'>bought price is required.");
		//$( "#msg2" ).dialog();
		$( "#msg2" ).dialog({buttons: { "Ok": function() { $(this).dialog("close"); } }, draggable: true, hide: "slide", show: "slide", width: 470 });
	}else if($('#unitPrice'+i).val() == '' ){
		$("#messages").html("<div id='msg3' title='Indika Stores'>Selling price is required.");
		//$( "#msg3" ).dialog();
		$( "#msg3" ).dialog({buttons: { "Ok": function() { $(this).dialog("close"); } }, draggable: true, hide: "slide", show: "slide", width: 470 });
	}else if($('#quantity'+i).val() == '' ){
		$("#messages").html("<div id='msg4' title='Indika Stores'>Quantity is required.");
		//$( "#msg4" ).dialog();
		$( "#msg4" ).dialog({buttons: { "Ok": function() { $(this).dialog("close"); } }, draggable: true, hide: "slide", show: "slide", width: 470 });
	}else if(mfd == '' && mfd == ''){
			addToStock(i);
	}else if(mfd == '' && exp != ''){
		if(checkDateFormat(exp))
			addToStock(i);
	}else if(mfd != '' && exp == ''){
		if(checkDateFormat(mfd))
			addToStock(i);
	}else if(mfd != '' && exp != ''){
		if(checkDateFormat(mfd) && checkDateFormat(exp)){
			if(Date.parse(mfd) < Date.parse(exp))
				addToStock(i);
			else{
				$("#messages").html("<div id='msg5' title='Indika Stores'>Expiry date must be greater than manufacture date.");
				//$( "#msg5" ).dialog();
				$( "#msg5" ).dialog({buttons: { "Ok": function() { $(this).dialog("close"); } }, draggable: true, hide: "slide", show: "slide", width: 470 });
			}
		}
	}
}
function checkDateFormat(date){
	var pattern = /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/;   //defining pattern for date
	if (pattern.test(date)) {
		return true;
	}else{
		$("#messages").html("<div id='msg6' title='Indika Stores'>Invalid date format.Please Input in (yyyy-mm-dd) format!");
		//$( "#msg6" ).dialog();
		$( "#msg6" ).dialog({buttons: { "Ok": function() { $(this).dialog("close"); } }, draggable: true, hide: "slide", show: "slide", width: 470 });
		return false;
	}
}
function validateTxt() {
	// Allow: backspace, delete, tab, escape, enter and .
	if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || event.keyCode == 190 || event.keyCode == 110 || 
		 // Allow: Ctrl+A
		(event.keyCode == 65 && event.ctrlKey === true) || 
		 // Allow: home, end, left, right
		(event.keyCode >= 35 && event.keyCode <= 39)) {
			 // let it happen, don't do anything
			 return;
	}
	else {
		if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
			event.preventDefault(); 
		}   
	}
}
function testForEnter() {
	if (event.keyCode == 13) {        
		event.cancelBubble = true;
		event.returnValue = false;
	}
}
function addToStock(i){
	itemCode['itemcode' + i] = $('#itemCode'+i).text();
	itemName['itemname' + i] = $('#itemName'+i).text();
	batchId['batchid' + i] = $("#batchId"+i+" option:selected").val();
	mfd['mfd' + i] = $("#mfd"+i).val();
	exp['exp' + i] = $("#exp"+i).val();
	boughtPrice['boughtprice' + i] = parseFloat($("#boughtPrice"+i).val());
	unitPrice['unitprice' + i] = parseFloat($("#unitPrice"+i).val());
	quantity['quantity'+i] = parseFloat($("#quantity"+i).val());
	
	if(!objCounter['objcounter'+i]){
		objCounter['objcounter' + i] = 1;
	}
	
	if(commonCounter == 0){
		var newRow = $(	"<tr><th>Item Name</th><th>Quantity</th><th>Amount</th><th>Delete</th></tr>");
		$('table.cart').append(newRow);
		commonCounter++;
	}
	if(objCounter['objcounter' + i] == 1){
		$("#displayStock #title").text("Stock Content");
		var newRow = $( "<tr id='row"+i+"'><td>"+itemName['itemname' + i]+" </td>"+
							"<td id='qtyId'>"+quantity['quantity' + i]+"</td>"+
							"<td><div id='amount"+i+"'></div>"+
								"<input type='hidden' name='itemCode"+i+"' value='"+itemCode['itemcode' + i]+"'/>"+
								"<input type='hidden' name='batchId"+i+"' value='"+batchId['batchid' + i]+"'/>"+
								"<input type='hidden' name='mfd"+i+"' value='"+mfd['mfd' + i]+"'/>"+
								"<input type='hidden' name='exp"+i+"' value='"+exp['exp' + i]+"'/>"+
								"<input type='hidden' name='boughtPrice"+i+"' value='"+boughtPrice['boughtprice' + i]+"'/>"+
								"<input type='hidden' name='unitPrice"+i+"' value='"+unitPrice['unitprice' + i]+"'/>"+
								"<input type='hidden' name='quantity"+i+"' value='"+quantity['quantity'+i]+"'/>"+
							"</td>"+
							"<td><button type='button' id='deleteBtn' onClick='deleteRow("+i+")'></button></td>"+
						"</tr>");
		$('table.cart').append(newRow);
		$("#displayStock #amount"+i).text(quantity['quantity'+i] * boughtPrice['boughtprice' + i]+" Rs");
		objCounter['objcounter' + i]++;
	}else{
		$("#displayStock #qtyId").text(quantity['quantity'+i]);
		$("#displayStock #amount"+i).text(quantity['quantity'+i] * boughtPrice['boughtprice' + i]+" Rs");
	}
	if(commonCounter > 0){
		$("#finish").html("<input id='submitBtn' type='submit' value='Add Stock'/>");
		totalPrice = totalPrice + (boughtPrice['boughtprice' + i] * quantity['quantity'+i]);
		$("#total").text("Total Price : "+totalPrice+" Rs/=");
	}
	dTable.fnFilter('');
}
function deleteRow(i){
	objCounter['objcounter' + i] = 1;
	totalPrice = totalPrice - (boughtPrice['boughtprice' + i] * quantity['quantity'+i]);
	$("#total").text("Total Price : "+totalPrice+" Rs/=");
	$("#displayStock #hidden").html("<td><input type='hidden' value='"+totalPrice+"' name='totalprice' /></td>");
	$("#finish").html("<input id='submitBtn' type='submit' value='Finish Order'/>");
	$("#row"+i).remove();
}
